// let baseUrl = "https://user.zaodiantao.com/api/v1/" //正式
let baseUrl = "https://user.test.zaodiantao.com/api/v1/" //测试
// let baseUrl = "http://192.168.3.106:8081/api/v1/" //测试
// let baseUrl = "http://192.168.3.29:8081/api/v1/" //测试
// let baseUrl = "http://192.168.3.194:8081/api/v1/" //测试
let chatUrl = "https://chat.zaodiantao.com/" //聊天列表正式
// let chatUrl = "https://chat.test.zaodiantao.com/" //聊天列表测试
let imgUrl = "https://img.zaodiantao.com/" //图片正式
// let imgUrl = "https://img.test.zaodiantao.com/" //图片测试
function Get(url,data,cb ){
  wx.request({
    url: baseUrl + url,
    method: "GET",
    data: data,
    header: {
      'content-type' : 'application/x-www-form-urlencoded',
      'Authorization': wx.getStorageSync('token')
    },
    success: (result) => {
      if(result.data.status == 1010 && !wx.getStorageSync('isLogin')){
        wx.setStorageSync('isLogin', true) // 登录
        wx.navigateTo({
          url: '/MemberPackage/login/login',
        })
      }
      typeof cb == 'function' && cb(result.data,"")
    },
    fail: (err) => {
      typeof cb == 'function' && cb(null,err.errMsg)
    },
  })
}
// function GetTwo(url,data,cb ){
//   wx.request({
//     url: baseUrl + url,
//     method: "GET",
//     data: data,
//     header: {
//       'content-type' : 'application/json',
//       'Authorization': wx.getStorageSync('token')
//     },
//     success: (result) => {
//       typeof cb == 'function' && cb(result.data,"")
//     },
//     fail: (err) => {
//       typeof cb == 'function' && cb(null,err.errMsg)
//     },
//   })
// }
function ChatGet(url,data,cb ){
  wx.request({
    url: chatUrl + url,
    method: "GET",
    data: data,
    header: {
      'content-type' : 'application/x-www-form-urlencoded',
      'Authorization': wx.getStorageSync('token')
    },
    success: (result) => {
      typeof cb == 'function' && cb(result.data,"")
    },
    fail: (err) => {
      typeof cb == 'function' && cb(null,err.errMsg)
    },
  })
}
function ChatPost(url ,data,cb ){
  wx.request({
    url: chatUrl + url,
    method: "POST",
    data: data,
    header: {
      'content-type' : 'application/x-www-form-urlencoded',
      'Authorization': wx.getStorageSync('token')
    },
    success: (result) => {
      typeof cb == 'function' && typeof cb(result.data,"")
    },
    fail: (err) => {
      typeof cb == 'function' && cb(null,err.errMsg)
    },
  })
}
function Post(url ,data,cb){
  // console.log(data)
  wx.request({
    url: baseUrl + url,
    method: "POST",
    data: data,
    header: {
      'content-type' : 'application/x-www-form-urlencoded',
      'Authorization': wx.getStorageSync('token')
    },
    success: (result) => {
      if(result.data.status == 1010 && !wx.getStorageSync('isLogin')){
        wx.setStorageSync('isLogin', true) // 登录
        wx.navigateTo({
          url: '/MemberPackage/login/login',
        })
      }
      typeof cb == 'function' && typeof cb(result.data,"")
    },
    fail: (err) => {
      typeof cb == 'function' && cb(null,err.errMsg)
    },
  })
}
function PostJson(url ,data,cb){
  wx.request({
    url: baseUrl + url,
    method: "POST",
    data: data,
    header: {
      'content-type' : 'application/json',
      'Authorization': wx.getStorageSync('token')
    },
    success: (result) => {
      if(result.data.status == 1010 && !wx.getStorageSync('isLogin')){
        wx.setStorageSync('isLogin', true) // 登录
        wx.navigateTo({
          url: '/MemberPackage/login/login',
        })
      }
      typeof cb == 'function' && typeof cb(result.data,"")
    },
    fail: (err) => {
      typeof cb == 'function' && cb(null,err.errMsg)
    },
  })
}
function Upload(url,filePath,data,cb ){
  wx.uploadFile({
    filePath: filePath,
    name: 'file',
    url: baseUrl + url,
    formData: data,
    header: {
      'content-type' : 'application/x-www-form-urlencoded',
      'Authorization': wx.getStorageSync('token')
    },
    success:(res) => {
      if (res.statusCode == 413) {
        wx.showToast({
          title: "图片大小限制5MB以内",
          icon: 'error'
        })
      }
       var data1 = JSON.parse(res.data)
       typeof cb == 'function' && typeof cb(data1,"")
    },fail: (err) => {
      typeof cb == 'function' && cb(null,err.errMsg)
    },
  })
}
module.exports = {
  baseUrl,
  imgUrl,
  Get:Get,
  chatUrl,
  // GetTwo:GetTwo,
  ChatGet:ChatGet,
  ChatPost:ChatPost,
  Post:Post,
  Upload:Upload,
  PostJson:PostJson
}